// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
//用于用户信息的操作userInfo
const db = uniCloud.database();
module.exports = {
	_before: function () { // 通用预处理器

	},
	//获取首页用户数据
	async getUserData(uid){
		let userData = await db.collection("uni-id-users")
		.doc(uid)
		.field({
			'nickname':true ,
			'username':true,
			'usersign':true,
			'count':true,
			'targetUniversity':true
		})
		.get()
		.catch(()=>{
					uni.showToast({
						title:"用户信息获取失败",
						icon:'error'
					})
				})
		return userData;
	},
	

	//获取用户历史记录限制20条
	async getUserHistory(uid){
		const dbJQL = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
					clientInfo: this.getClientInfo()
				});
				
		const UserHistoryData= await dbJQL.collection("history_person")
		.where({
			uid:uid
		})
		.field("Chapterid,historyCreateTime,qOrder,qid,subject_id,ChapterName,subjectName,historyCreateTime")
		.orderBy("historyCreateTime","desc")
		.limit(20).get({
			getCount:true
		});
		return UserHistoryData;
	},
	//获取用户历史记录 无限制
	async getUserHistoryNolim(uid){
		const dbJQL = uniCloud.databaseForJQL({ // 获取JQL database引用，此处需要传入云对象的clientInfo
					clientInfo: this.getClientInfo()
				});
				
		const UserHistoryData= await dbJQL.collection("history_person")
		.where({
			uid:uid
		})
		.field("Chapterid,historyCreateTime,qOrder,qid,subject_id,ChapterName,subjectName,historyCreateTime")
		.orderBy("historyCreateTime","desc")
		.get({
			getCount:true
		});
		return UserHistoryData;
	},
	
	
	
	//删除用户历史记录
	async delHistory(hid){
		const delHistoryRes = db.collection("history_person")
		.doc(hid).remove();
		return delHistoryRes;
	},
	//显示刷题正确率
	// async getAccuracyRate(){
	// 	const right
	// },
	//获取刷题量正确率
	async getUserBrushesData(uid){
		const brushesData = await db.collection("userBrushes")
		.where({
			uid:uid
		}).get();
		return brushesData;
	},
	//用于判断刷题量数据库是否需要更新
	async decideUserBrushes(uid,brushCount,accuracyRate){
		
	}
	
}
